package com.sky.zero.common.generate.core;


public class GenerateMapper extends GenerateBase {

    public GenerateMapper(GenerateBase generateBase) {
        super(generateBase.packagePath, generateBase.tableName, generateBase.moduleName, generateBase.className,
                generateBase.fieldList, generateBase.fieldMethod);
    }

    public void execute() {
        StringBuilder builder = new StringBuilder();
        String generatePackage = generatePackage(packagePath + "." + moduleName + ".mapper");
        String generateClass = generateClass(className);
//        String generateAttribute = generateAttribute(className);

        builder.append(generatePackage);
        builder.append(generateMapperImport(generateImport(generateClass), packagePath, moduleName,
                className));
        builder.append(generateClass);
//        builder.append(generateAttribute);

        writeGenerateFile(builder.append("}").toString().getBytes(), getGeneratePath("mapper", "Mapper"));

    }

    /**
     * 生成引用路径
     **/
    public String generateMapperImport(String importJavaPath, String packagePath, String moduleName, String className) {
        return importJavaPath + "import " + packagePath + "." + moduleName + "." + "entity" + "." + className + ";\n"
                + "import " + packagePath + "." + moduleName + "." + "entity" + "." + className + "Param;\n" + "\n\n" ;
    }

    /**
     * 生成类
     **/
    public String generateClass(String className) {
        return "public interface " + className + "Mapper extends BaseMapper<" + className + ", " + className + "Param> {\n\n" ;
    }

    /**
     * 生成接口方法
     **/
    public String generateAttribute(String className) {
        StringBuilder builder = new StringBuilder();
        if (fieldInfo != null) {
            if (fieldInfo.getComment() != null) {
                builder.append("	/** 根据").append(fieldInfo.getComment()).append("查询 **/\n");
            }
            builder.append("	").append(className).append(" findBy").append(upper(fieldInfo.getJavaColumnName()))
                    .append("(").append(fieldInfo.getJavaDataType()).append(" ").append(fieldInfo.getJavaColumnName())
                    .append(");\n\n");

            if (fieldInfo.getComment() != null) {
                builder.append("	/** 根据").append(fieldInfo.getComment()).append("物理删除 **/\n");
            }
            builder.append("	").append("int delBy").append(upper(fieldInfo.getJavaColumnName())).append("(")
                    .append(fieldInfo.getJavaDataType()).append(" ").append(fieldInfo.getJavaColumnName())
                    .append(");\n\n");

            builder.append("	/** 批量删除 **/\n");
            builder.append("	").append("int delByIds").append("(List<").append(fieldInfo.getJavaDataType()).append("> list")
                    .append(");\n\n");
        }

        builder.append("	/** 插入记录 **/\n");
        builder.append("	").append("int insert").append("(").append(className).append(" ").append(lower(className))
                .append(");\n\n");

        builder.append("	/** 批量插入记录 **/\n");
        builder.append("	").append("int insertBatch").append("(List<").append(className).append("> list")
                .append(");\n\n");

        builder.append("	/** 更新记录 **/\n");
        builder.append("	").append("int update").append("(").append(className).append(" ").append(lower(className))
                .append(");\n\n");

        builder.append("	/** 批量更新记录 **/\n");
        builder.append("	").append("int update").append("(List<").append(className).append("> list")
                .append(");\n\n");

        builder.append("	/** 查询列表 **/\n");
        builder.append("    List<").append(className).append("> findList(")
                .append(className).append("Param param);\n\n");
        return builder.toString();
    }

}
